<?php
class Order_Model_Product
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_Product();
	}

	/**
	*Create A Record  
	*@param rowSet 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}

	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $op_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto("op_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	/**
	*Delete One Row
	*@param int $op_id 
	*@return boolean
	*/
	public function delete($id){
		$where = $this->_table->getAdapter()->quoteInto("op_id= ?", $id);
		return $this->_table->delete($where);
	}

    public function getByCondition($condition=array(),$type='*',$orderby=array('op_id'),$pageSize=0,$page=1){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		
		$select->from($table,$type);
		$select->where("1 =?",1);
		if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
			$select->where(" orders_code = ?",$condition["orders_code"]);
		}
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
			$select->where(" product_id = ?",$condition["product_id"]);
		}
		if(isset($condition["product_ids"]) && $condition["product_ids"]!=''){
			$select->where(" product_id in(?)",$condition["product_ids"]);
		}
		if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
			$select->where(" product_sku = ?",$condition["product_sku"]);
		}
		if(isset($condition["op_title"]) && $condition["op_title"]!=''){
			$select->where(" op_title like ?","%".$condition["op_title"]."%");
		}
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			return $this->_table->getAdapter()->fetchAll($sql);
		}
	}

	/**
	*Get Rowset  
	*@param string $op_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('op_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	
	/**
	 * 获取订单预期重量
	 */
	public  function  getOrderWeightByOrdercode($Ordercode){
		$where = $this->_table->getAdapter()->quoteInto('orders_code= ?',$Ordercode);
		$rows =$this->_table->fetchAll($where);
		$weight=0;
		if($rows){
			
			foreach ($rows as $val){
				$weight+=$val['op_weight']*$val['op_quantity'];
			}
		}
		return $weight;
	
	}
	
	
}